package com.sh;

import java.awt.*;

/**
 * @author lishuhua
 * @Description:
 * @date 2021-03-16 23:08
 */
public class Tank {

    private int x,y;

    private Dir dir = Dir.DOWN;

    private static final int SPEED = 5;

    public static int WIDTH = ResourceMgr.goodTankD.getWidth();

    public static int HEIGHT = ResourceMgr.goodTankD.getHeight();

    private boolean moving = false;

    private TankFrame tankFrame = null;

    public Tank(int x, int y, Dir dir,TankFrame tankFrame) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.tankFrame = tankFrame;
    }

    public void paint(Graphics g) {

        Color c = g.getColor();

        switch (dir) {

            case LEFT:
                g.drawImage(ResourceMgr.goodTankL,x,y,null);
                break;
            case UP:
                g.drawImage(ResourceMgr.goodTankU,x,y,null);
                break;
            case RIGHT:
                g.drawImage(ResourceMgr.goodTankR,x,y,null);
                break;
            case DOWN:
                g.drawImage(ResourceMgr.goodTankD,x,y,null);
                break;
        }


        move();
    }

    private void move() {

        if(!moving) return;

        switch (dir) {

            case LEFT:
                x -= SPEED;
                break;
            case UP:
                y -= SPEED;
                break;
            case RIGHT:
                x += SPEED;
                break;
            case DOWN:
                y += SPEED;
                break;
        }
    }

    public boolean isMoving() {
        return moving;
    }

    public void setMoving(boolean moving) {
        this.moving = moving;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public Dir getDir() {
        return dir;
    }

    public void setDir(Dir dir) {
        this.dir = dir;
    }

    public static int getSPEED() {
        return SPEED;
    }

    public void fire() {

        int bX = this.x + Tank.WIDTH/2 - Bullet.WIDTH/2;
        int bY = this.y + Tank.HEIGHT/2 - Bullet.HEIGHT/2;
        tankFrame.bullets.add(new Bullet(bX, bY, this.dir, this.tankFrame));
    }
}
